package com.anlen.call.ui;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.anlen.call.R;
import com.anlen.call.util.RefreshableView;
@SuppressLint("InlinedApi")
public class NewsFragment extends Fragment {

	private static final String ARG_POSITION = "position";
	RefreshableView refreshableView;
	private int position;
	private ListView listview =null;
	private List<Map<String, Object>> data;

	public static NewsFragment newInstance(int position) {
		NewsFragment f = new NewsFragment();
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		position = getArguments().getInt(ARG_POSITION);
	
		
	}

	@SuppressLint("InlinedApi")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

		FrameLayout fl = new FrameLayout(getActivity());
		fl.setLayoutParams(params);

		
		switch (position) {
			case 0:
				final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
					.getDisplayMetrics());
	
				TextView v = new TextView(getActivity());
				params.setMargins(margin, margin, margin, margin);
				v.setLayoutParams(params);
				v.setGravity(Gravity.CENTER);
				v.setText("PAGE " + (position + 1));	
				fl.addView(v);
				break;
			case 1:
				ImageView iv=new ImageView(getActivity());
				iv.setImageDrawable(getResources().getDrawable(com.anlen.call.R.drawable.welcome2));
				fl.addView(iv);
				break;
			case 2:	
				View view = inflater.inflate(R.layout.listview_adapter, container,false);
				listview = (ListView) view.findViewById(R.id.lvArray);
				putData();
				fl.addView(view);
				break;
			default:
				break;
		}
		return fl;
	}
	private void putData()
	{  
	        /*SimpleAdapter的参数说明 
         * 第一个参数 表示访问整个android应用程序接口，基本上所有的组件都需要 
         * 第二个参数表示生成一个Map(String ,Object)列表选项 
         * 第三个参数表示界面布局的id  表示该文件作为列表项的组件 
         * 第四个参数表示该Map对象的哪些key对应value来生成列表项 
         * 第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系 
         * 注意的是map对象可以key可以找不到 但组件的必须要有资源填充  因为 找不到key也会返回null 其实就相当于给了一个null资源 
         * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
         * 这个head的组件会被name资源覆盖 
         * */  
	
		data=new ArrayList<Map<String,Object>>();
		
		Map<String, Object> map1=new HashMap<String, Object>();
		map1.put("ivIcon",R.drawable.jianai);
		map1.put("tvName", "简爱");
		map1.put("tvMotto", "风将绿了夜的途");
		
		Map<String, Object> map2=new HashMap<String, Object>();
		map2.put("ivIcon", R.drawable.momo);
		map2.put("tvName", "陌陌");
		map2.put("tvMotto", "寻找你,你在我心中__。");
		
		Map<String, Object> map3=new HashMap<String, Object>();
		map3.put("ivIcon", R.drawable.xiyan);
		map3.put("tvName", "汐颜");
		map3.put("tvMotto", "最新分享:中国合伙人正能量.");
		
		Map<String, Object> map4=new HashMap<String, Object>();
		map4.put("ivIcon", R.drawable.huaxianzi);
		map4.put("tvName", "花仙子");
		map4.put("tvMotto", "貌美如花");
		
		Map<String, Object> map5=new HashMap<String, Object>();
		map5.put("ivIcon", R.drawable.huaxianzi);
		map5.put("tvName", "花仙");
		map5.put("tvMotto", "美如花");
		
		Map<String, Object> map6=new HashMap<String, Object>();
		map6.put("ivIcon", R.drawable.huaxianzi);
		map6.put("tvName", "花仙子");
		map6.put("tvMotto", "貌美如花");
		
		Map<String, Object> map7=new HashMap<String, Object>();
		map7.put("ivIcon", R.drawable.huaxianzi);
		map7.put("tvName", "花仙子");
		map7.put("tvMotto", "貌美如花");
		
		Map<String, Object> map8=new HashMap<String, Object>();
		map8.put("ivIcon", R.drawable.huaxianzi);
		map8.put("tvName", "花仙子");
		map8.put("tvMotto", "貌美如花");
		
		Map<String, Object> map9=new HashMap<String, Object>();
		map9.put("ivIcon", R.drawable.huaxianzi);
		map9.put("tvName", "花仙子");
		map9.put("tvMotto", "貌美如花");
		
		Map<String, Object> map10=new HashMap<String, Object>();
		map10.put("ivIcon", R.drawable.huaxianzi);
		map10.put("tvName", "花仙子");
		map10.put("tvMotto", "貌美如花");
		
		Map<String, Object> map11=new HashMap<String, Object>();
		map11.put("ivIcon", R.drawable.huaxianzi);
		map11.put("tvName", "花仙子");
		map11.put("tvMotto", "貌美如花");
		
		Map<String, Object> map12=new HashMap<String, Object>();
		map12.put("ivIcon", R.drawable.huaxianzi);
		map12.put("tvName", "花仙子");
		map12.put("tvMotto", "貌美如花");
		
		Map<String, Object> map13=new HashMap<String, Object>();
		map13.put("ivIcon", R.drawable.huaxianzi);
		map13.put("tvName", "花仙子");
		map13.put("tvMotto", "貌美如花");
		
		Map<String, Object> map14=new HashMap<String, Object>();
		map14.put("ivIcon", R.drawable.huaxianzi);
		map14.put("tvName", "花仙子");
		map14.put("tvMotto", "貌美如花");
		
		Map<String, Object> map15=new HashMap<String, Object>();
		map15.put("ivIcon", R.drawable.huaxianzi);
		map15.put("tvName", "花仙子");
		map15.put("tvMotto", "貌美如花");
		
		Map<String, Object> map16=new HashMap<String, Object>();
		map16.put("ivIcon", R.drawable.huaxianzi);
		map16.put("tvName", "花仙子");
		map16.put("tvMotto", "貌美如花");
		
		Map<String, Object> map17=new HashMap<String, Object>();
		map17.put("ivIcon", R.drawable.huaxianzi);
		map17.put("tvName", "花仙子");
		map17.put("tvMotto", "貌美如花");
		
		Map<String, Object> map18=new HashMap<String, Object>();
		map18.put("ivIcon", R.drawable.huaxianzi);
		map18.put("tvName", "花仙子");
		map18.put("tvMotto", "貌美如花");
		
		Map<String, Object> map19=new HashMap<String, Object>();
		map19.put("ivIcon", R.drawable.huaxianzi);
		map19.put("tvName", "花仙子");
		map19.put("tvMotto", "貌美如花");
		
		Map<String, Object> map20=new HashMap<String, Object>();
		map20.put("ivIcon", R.drawable.huaxianzi);
		map20.put("tvName", "花仙子");
		map20.put("tvMotto", "貌美如花");
		
		data.add(map1);
		data.add(map2);
		data.add(map3);
		data.add(map4);	
		data.add(map5);
		data.add(map6);
		data.add(map7);
		data.add(map8);	
		data.add(map9);
		data.add(map10);
		data.add(map11);
		data.add(map12);	
		data.add(map13);
		data.add(map14);
		data.add(map15);
		data.add(map16);	
		data.add(map17);
		data.add(map18);
		data.add(map19);
		data.add(map20);	
		
		ListAdapter simpleAdapter = new SimpleAdapter(
				getActivity(),
				data,
				R.layout.item,
				new String[]{"ivIcon","tvName","tvMotto"},
				new int[]{R.id.ivIcon,R.id.tvName,R.id.tvMotto}
		);
		listview.setAdapter(simpleAdapter);
	}

}